{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e9c676c0",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/data_connectors/GoogleDriveDemo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88141371-de4c-4c02-9e8f-10d2491b5a33",
   "metadata": {},
   "source": [
    "# Google Drive Reader\n",
    "Demonstrates our Google Drive data connector"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2efe966",
   "metadata": {},
   "source": [
    "## Prerequisites "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa083b9f",
   "metadata": {},
   "source": [
    "Follow [these steps](https://developers.google.com/drive/api/quickstart/python#set_up_your_environment) to setup your environment.\n",
    "1. Enable the Google Drive API in your GCP project.\n",
    "1. Configure an OAuth Consent screen for your GCP project.\n",
    "    * It is fine to make it \"External\" if you're not in a Google Workspace.\n",
    "1. Create client credentials for your application (this notebook).\n",
    "    * Make sure to use \"Desktop app\" as the application type.\n",
    "    * Move these client credentials to the directory this notebook is in, and name it \"credentials.json\"."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "018d3fd6",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea0e003b",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index llama-index-readers-google"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6b62adf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "import sys\n",
    "\n",
    "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
    "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fafc701d-5a10-4503-9dae-22698fb1aee9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core import SummaryIndex\n",
    "from llama_index.readers.google import GoogleDriveReader\n",
    "from IPython.display import Markdown, display"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d14d45e1",
   "metadata": {},
   "source": [
    "## Choose Folder to Read"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e0667e6",
   "metadata": {},
   "source": [
    "You can find a folder ID by navigating to a folder in Google Drive then selecting the last part of the URL.\n",
    "\n",
    "For example, with this URL: `https://drive.google.com/drive/u/0/folders/abcdefgh12345678` the folder ID is `abcdefgh12345678`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8f0a9f2-c6a9-4840-a38a-0b2f8e433063",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Replace the placeholder with your chosen folder ID\n",
    "folder_id = [\"<your_folder_id>\"]\n",
    "\n",
    "# Make sure credentials.json file exists in the current directory (data_connectors)\n",
    "documents = GoogleDriveReader().load_data(folder_id=folder_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89ef1fac-aa36-4a5f-b5cf-bc4dfa0bd332",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = SummaryIndex.from_documents(documents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2c1573f-2e49-49e8-8daf-19e6f1777eaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set Logging to DEBUG for more detailed outputs\n",
    "query_engine = index.as_query_engine()\n",
    "response = query_engine.query(\"<query_text>\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d4533c9-9020-4f50-837c-316ec2c454f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
